// shopPages/mygenjin/mygenjin.js
import * as echarts from '../ec-canvas/echarts';

const app = getApp();
var startDay = ''
var endDay = ''

var dateType = 'ALL_DAY'
var canvastest = '';
var widthtest = '';
var heighttest = '';

function initChart(canvas, width, height) {


  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvastest = canvas;
  widthtest = width;
  heighttest = height;
  canvas.setChart(chart);

  wx.request({
    url: app.globalData.bossradar + '/api/1.0/getConversionRate',
    data: {
      // 
      uid: app.globalData.userInfo.id,
      isBoss: false,
      dateType: dateType,
      startDay: '',
      endDay: ''
    },
    method: 'POST',
    header: {
      'content-type': 'application/json' // 默认值
    },
    success(res) {

      // console.log(parseInt(res.data.data.potentialUserCountResult)*100);
      // console.log(parseInt(res.data.data.intentionalUserCountResult) * 100);
      // console.log(parseInt(res.data.data.customerUserCountResult) * 100);
     var option = {
        // title: {
        //   text: '漏斗图',
        //   subtext: '纯属虚构'
        // },
       color: ["#37A2DA", "#32C5E9", "#67E0E3"],
        tooltip: {
          trigger: 'item',
          formatter: "{a} <br/>{b} : {c}%"
        },
        toolbox: {
          feature: {
          //  dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        legend: {
          "textStyle": {
            "fontSize": 16
          },
          data: ['潜在客户' + res.data.data.potentialUserCountResult, '意向客户' + res.data.data.intentionalUserCountResult, '客户' + res.data.data.customerUserCountResult]
        },
        calculable: true,
        series: [
          {
            name: '漏斗图',
            type: 'funnel',
            width: '40%',
            height: '70%',
            left: '10%',
            
            "textStyle": {
              "fontSize": 16,
            },
            width: '80%',
            // height: {totalHeight} - y - y2,
            min: 0,
            //max: 100,
            minSize: '0%',
            maxSize: '100%',
            sort: 'descending',
          //  gap: 2,
            label: {
              show: true,
              position: 'inside'
            },
            labelLine: {
              length: 10,
              orient: 'vertical',
              // lineStyle: {
              //   width: 1,
              //   type: 'solid'
              // }
            },
            itemStyle: {
              borderColor: '#fff',
              borderWidth: 1
            },
            emphasis: {
              label: {
                fontSize: 12,
                show: true,
                position: 'inside'
              }
            },
            data: [
              { value: res.data.data.potentialUserCountResult, name: '潜在客户' + res.data.data.potentialUserCountResult },
              { value: res.data.data.intentionalUserCountResult, name: '意向客户' + res.data.data.intentionalUserCountResult },
              { value: res.data.data.customerUserCountResult, name: '客户' + res.data.data.customerUserCountResult }
            ]
          }
        ]
      };
  


      chart.setOption(option);
      return chart;
    }
  })

}

let chart;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tab: ['累计', '昨日'],
    taskBack:false,//遮罩隐藏
    curren: 0,
    tabchart: ['累计', '今天', '昨日', '近七日'],
    c_curren: 0,
    ec: {
      onInit: initChart
    },
    start:'',
    end:'',
    newlist:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var day3 = new Date();
    day3.setTime(day3.getTime());
    var month = day3.getMonth() + 1;
    if (month.toString().length == 1) {
      month = '0' + month;
    }
    var day = day3.getDate();
    if (day.toString().length == 1) {
      day = '0' + day;
    }
    var start = day3.getFullYear() + "-" + month + "-01";
    var end = day3.getFullYear() + "-" + month + "-" + day;
    that.setData({
      start: start,
      end: end
    });
    dateType = 'ALL_DAY'
    that.getmyData(start, end)
  },
  getmyData(){
    var that = this
    wx.request({
      url: app.globalData.bossradar +'/api/1.0/getStatics',
      data: {
        // app.globalData.userInfo.id
        uid: app.globalData.userInfo.id,
        isBoss:false,
        dateType: dateType,
        startDay: startDay,
        endDay: endDay
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        console.log(res)
        that.setData({
          newlist:res.data.data.count
        })
      },
      fail: function(res) {
        console.log(">>>>>>>>>>>失败")
      },
      complete: function(res) {},
    })
  },
  bindtab(e) {
    var index = e.currentTarget.id;
    this.setData({
      curren: index,
      taskBack: false
    })
    if (index == 0) {
      dateType = 'ALL_DAY'
    } else if (index == 1) {
      dateType = 'YESTERDAY'
    } 
    this.getmyData()
  },
  bindtab1(e) {
    var index = e.currentTarget.id;
    this.setData({
      c_curren: index
    })
    if (index == 0) {
      dateType = 'ALL_DAY'
    } else if (index == 1) {
      dateType = 'TODAY'
    } else if (index == 2) {
      dateType = 'YESTERDAY'
    } else if (index == 3) {
      dateType = 'WEEK'
    }
    
    initChart(canvastest, widthtest, heighttest);
  },
  bindhide() {
    var taskBack = this.data.taskBack
    this.setData({
      taskBack: true
    })
  },
  getData(){
    var that = this
    var taskBack = that.data.taskBack
    that.getmyData(that.data.start, that.data.end)
    that.setData({
      taskBack: false
    })
    dateType = 'SELECT_DAY'
    startDay = that.data.start
    endDay = that.data.end
    that.getmyData()
  },
  bindDateChange: function (e) {
    this.setData({
      start: e.detail.value
    })
  },
  bindDateChange1: function (e) {
    this.setData({
      end: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // },
  myHiddenCustomer: function() {
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=0' + '&fromFlag=false',
    })
  },
  myIntentionCustomer: function() {
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=1' + '&fromFlag=false',
    })
  },

  myCustomer: function() {
    wx.navigateTo({
      url: '/shopPages/Customerlist/Customerlist?flag=1&dateType=' + dateType + '&startDay=' + startDay + '&endDay=' + endDay + '&logType=2' + '&fromFlag=false',
    })
  }

})